package icu.wangrui.bookstore.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Random;

@Component
public class SendEmail {
    //注入邮件发送
    @Autowired
    JavaMailSenderImpl javaMailSender;

    public void sendMail(boolean multipart,String subject,String text,String sendTo) throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //组装 开启
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, multipart);
        //正文
        helper.setSubject(subject);
        helper.setText(text,true);
        //附件
        //helper.addAttachment("test.jpg",new File("C:\\Users\\Administrator\\Desktop\\1.jpg"));

        helper.setTo(sendTo);//发给谁
        helper.setFrom("260474420@qq.com");//邮件来自谁
        javaMailSender.send(mimeMessage);
    }
    //产生随机码
    public String getCode() {
        String str = "";
        Random ra = new Random();
        for (int i = 0; i < 6; i++) {
            int num = ra.nextInt(10);
            str+=num;
        }
        return str;
    }

}
